clear all
log close
log using "Judicial Selection and Criminal Punishment Log", replace text
use "Judicial Selection and Criminal Punishment Data.dta"

********************************************************************************
********************************************************************************
*****  	            Judicial Selection and Criminal Punishment:			   *****
*****  Trial Court Elections, Sentencing, and Incarceration in the States  *****
*****                       Author: Travis N. Taylor                       *****
*****                       Last Edited: 12 NOV 2020                       *****
********************************************************************************
********************************************************************************

xtset stateid year 

****************
*** Figure 1 ***
****************

	twoway (tsline avg_incarcerationrate100k) (tsline avg_sentencerate100k)

****************
*** Figure 2 ***
****************

	twoway (tsline sentencerate100k if stateid==01) (tsline incarcerationrate100k if stateid ==01), saving(AL.gph)
	twoway (tsline sentencerate100k if stateid==02) (tsline incarcerationrate100k if stateid ==02), saving(AK.gph)
	twoway (tsline sentencerate100k if stateid==04) (tsline incarcerationrate100k if stateid ==04), saving(AZ.gph)
	twoway (tsline sentencerate100k if stateid==05) (tsline incarcerationrate100k if stateid ==05), saving(AR.gph)
	twoway (tsline sentencerate100k if stateid==06) (tsline incarcerationrate100k if stateid ==06), saving(CA.gph)
	twoway (tsline sentencerate100k if stateid==08) (tsline incarcerationrate100k if stateid ==08), saving(CO.gph)
	twoway (tsline sentencerate100k if stateid==09) (tsline incarcerationrate100k if stateid ==09), saving(CT.gph)
	twoway (tsline sentencerate100k if stateid==10) (tsline incarcerationrate100k if stateid ==10), saving(DE.gph)
	twoway (tsline sentencerate100k if stateid==12) (tsline incarcerationrate100k if stateid ==12), saving(FL.gph)
	twoway (tsline sentencerate100k if stateid==13) (tsline incarcerationrate100k if stateid ==13), saving(GA.gph)
	twoway (tsline sentencerate100k if stateid==15) (tsline incarcerationrate100k if stateid ==15), saving(HI.gph)
	twoway (tsline sentencerate100k if stateid==16) (tsline incarcerationrate100k if stateid ==16), saving(ID.gph)
	twoway (tsline sentencerate100k if stateid==17) (tsline incarcerationrate100k if stateid ==17), saving(IL.gph)
	twoway (tsline sentencerate100k if stateid==18) (tsline incarcerationrate100k if stateid ==18), saving(IN.gph)
	twoway (tsline sentencerate100k if stateid==19) (tsline incarcerationrate100k if stateid ==19), saving(IO.gph)
	twoway (tsline sentencerate100k if stateid==20) (tsline incarcerationrate100k if stateid ==20), saving(KS.gph)
	twoway (tsline sentencerate100k if stateid==21) (tsline incarcerationrate100k if stateid ==21), saving(KY.gph)
	twoway (tsline sentencerate100k if stateid==22) (tsline incarcerationrate100k if stateid ==22), saving(LA.gph)
	twoway (tsline sentencerate100k if stateid==23) (tsline incarcerationrate100k if stateid ==23), saving(ME.gph)
	twoway (tsline sentencerate100k if stateid==24) (tsline incarcerationrate100k if stateid ==24), saving(MD.gph)
	twoway (tsline sentencerate100k if stateid==25) (tsline incarcerationrate100k if stateid ==25), saving(MA.gph)
	twoway (tsline sentencerate100k if stateid==26) (tsline incarcerationrate100k if stateid ==26), saving(MI.gph)
	twoway (tsline sentencerate100k if stateid==27) (tsline incarcerationrate100k if stateid ==27), saving(MN.gph)
	twoway (tsline sentencerate100k if stateid==28) (tsline incarcerationrate100k if stateid ==28), saving(MS.gph)
	twoway (tsline sentencerate100k if stateid==29) (tsline incarcerationrate100k if stateid ==29), saving(MO.gph)
	twoway (tsline sentencerate100k if stateid==30) (tsline incarcerationrate100k if stateid ==30), saving(MT.gph)
	twoway (tsline sentencerate100k if stateid==31) (tsline incarcerationrate100k if stateid ==31), saving(NE.gph)
	twoway (tsline sentencerate100k if stateid==32) (tsline incarcerationrate100k if stateid ==32), saving(NV.gph)
	twoway (tsline sentencerate100k if stateid==33) (tsline incarcerationrate100k if stateid ==33), saving(NH.gph)
	twoway (tsline sentencerate100k if stateid==34) (tsline incarcerationrate100k if stateid ==34), saving(NJ.gph)
	twoway (tsline sentencerate100k if stateid==35) (tsline incarcerationrate100k if stateid ==35), saving(NM.gph)
	twoway (tsline sentencerate100k if stateid==36) (tsline incarcerationrate100k if stateid ==36), saving(NY.gph)
	twoway (tsline sentencerate100k if stateid==37) (tsline incarcerationrate100k if stateid ==37), saving(NC.gph)
	twoway (tsline sentencerate100k if stateid==38) (tsline incarcerationrate100k if stateid ==38), saving(ND.gph)
	twoway (tsline sentencerate100k if stateid==39) (tsline incarcerationrate100k if stateid ==39), saving(OH.gph)
	twoway (tsline sentencerate100k if stateid==40) (tsline incarcerationrate100k if stateid ==40), saving(OK.gph)
	twoway (tsline sentencerate100k if stateid==41) (tsline incarcerationrate100k if stateid ==41), saving(OR.gph)
	twoway (tsline sentencerate100k if stateid==42) (tsline incarcerationrate100k if stateid ==42), saving(PA.gph)
	twoway (tsline sentencerate100k if stateid==44) (tsline incarcerationrate100k if stateid ==44), saving(RI.gph)
	twoway (tsline sentencerate100k if stateid==45) (tsline incarcerationrate100k if stateid ==45), saving(SC.gph)
	twoway (tsline sentencerate100k if stateid==46) (tsline incarcerationrate100k if stateid ==46), saving(SD.gph)
	twoway (tsline sentencerate100k if stateid==47) (tsline incarcerationrate100k if stateid ==47), saving(TN.gph)
	twoway (tsline sentencerate100k if stateid==48) (tsline incarcerationrate100k if stateid ==48), saving(TX.gph)
	twoway (tsline sentencerate100k if stateid==49) (tsline incarcerationrate100k if stateid ==49), saving(UT.gph)
	twoway (tsline sentencerate100k if stateid==50) (tsline incarcerationrate100k if stateid ==50), saving(VT.gph)
	twoway (tsline sentencerate100k if stateid==51) (tsline incarcerationrate100k if stateid ==51), saving(VA.gph)
	twoway (tsline sentencerate100k if stateid==53) (tsline incarcerationrate100k if stateid ==53), saving(WA.gph)
	twoway (tsline sentencerate100k if stateid==54) (tsline incarcerationrate100k if stateid ==54), saving(WV.gph)
	twoway (tsline sentencerate100k if stateid==55) (tsline incarcerationrate100k if stateid ==55), saving(WI.gph)
	twoway (tsline sentencerate100k if stateid==56) (tsline incarcerationrate100k if stateid ==56), saving(WY.gph)

	graph combine AL.gph AK.gph AZ.gph AR.gph CA.gph CO.gph CT.gph DE.gph FL.gph ///
	GA.gph HI.gph ID.gph IL.gph IN.gph IO.gph KS.gph KY.gph LA.gph ME.gph MD.gph ///
	MA.gph MI.gph MN.gph MS.gph MO.gph MT.gph NE.gph NV.gph NH.gph NJ.gph NM.gph ///
	NY.gph NC.gph ND.gph OH.gph OK.gph OR.gph PA.gph RI.gph SC.gph SD.gph TN.gph ///
	TX.gph UT.gph VT.gph VA.gph WA.gph WV.gph WI.gph WY.gph 
	
***************
*** Table 2 ***
***************
	
xtsum incarcerationrate_change
xtsum sentencerate_change
tab retentionelex
xtsum berrycitizen_l3a
xtsum arrestrate_l1 
tab noiac 
tab dsl 
xtsum mand_min 
tab sentencecomm  
tab electedpros 
xtsum termlength 
tab recall 
tab divgov 
xtsum berrygovt
tab south 
xtsum blackpopulation100k
xtsum pctbach
xtsum stateincome_l1

******************
*** Footnote 5 ***
******************

*Stationary tests

xtunitroot ips incarcerationrate100k
xtcointtest kao incarcerationrate100k retentionelex

xtunitroot ips sentencerate100k
xtcointtest kao sentencerate100k retentionelex


**********************************
*** Main Models: Incarceration ***
**********************************

xtpcse incarcerationrate_change i.retentionelex c.berrycitizen_l3a 			 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm incarcerationrate_change_l1 ///
	electedpros termlength recall divgov berrygovt							 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)
	
	estimates store model1
	
outreg2 using 																 ///
	"/Users/Travis/Dropbox/Research/Judicial Selection and Criminal Punishment/Figures/v3 Figures/Table3.txt",   ///
	word nonotes title(Table 3: Antecedents of State Incarceration Rate)     ///
	lab bdec(2)																 ///
	onecol ctitle("Model 1")												 /// 
	alpha (.01, .05, .1) symbol(***, **, *) 								 ///
	addnote (All models estimated using Stata 15.0's xtpcse command. Data    ///
	from National Prisoner Statistics, 1978-2015, US Department of Justice,  ///
	ICPSR Study 36657. DV in all models is a state's incarceration rate		 ///
	per 100,000 population. Correlated panels corrected standard errors in 	 ///
	parentheses. Results of two–tailed tests of significance are as 	 	 ///
	follows: *p< .1 **p< .05 ***p< .01. Autocorrelation corrected using      ///
	panel-specific AR(1). Unit of analysis is the state-year.) replace
	
xtpcse incarcerationrate_change i.retentionelex##c.berrycitizen_l3a 		 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm incarcerationrate_change_l1 ///
	electedpros termlength recall divgov berrygovt							 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)
	
	estimates store model2
	
outreg2 using 																 ///
	"/Users/Travis/Dropbox/Research/Judicial Selection and Criminal Punishment/Figures/v3 Figures/Table3.txt",   ///
	word nonotes title(Table 3: Antecedents of State Incarceration Rate)     ///
	lab bdec(2)																 ///
	onecol ctitle("Model 2")												 /// 
	alpha (.01, .05, .1) symbol(***, **, *) 								 ///
	addnote (All models estimated using Stata 15.0's xtpcse command. Data    ///
	from National Prisoner Statistics, 1978-2015, US Department of Justice,  ///
	ICPSR Study 36657. DV in all models is a state's incarceration rate		 ///
	per 100,000 population. Correlated panels corrected standard errors in 	 ///
	parentheses. Results of two–tailed tests of significance are as 	 	 ///
	follows: *p< .1 **p< .05 ***p< .01. Autocorrelation corrected using      ///
	panel-specific AR(1). Unit of analysis is the state-year.) append

*** Margins
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(1)1))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(2)2))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(3)3))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(2(1)3))

*** Contrasts

*contrasts (are the differences between selection type signif diff at .05 level?)
*comparing appt vs partisan
	margins r(0(1)1).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F4a)
	margins r(0(2)2).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F4b)
	margins r(0(3)3).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F4c)

	graph combine F4a.gph F4b.gph F4c.gph, saving(Figure4)
	
*********************************************************
*** Non-Partisan v. Partisan Elections: Incarceration ***
*********************************************************

xtpcse incarcerationrate_change i.retentionelex##c.berrycitizen_l3a 		 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm incarcerationrate_change_l1 ///
	electedpros termlength recall divgov berrygovt							 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)

	margins r(2(1)3).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F5)
	
*******************************
*** Main Models: Sentencing ***
*******************************
	
xtpcse sentencerate_change i.retentionelex c.berrycitizen_l3a 		 		 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm sentencerate_change_l1	 ///
	electedpros termlength recall divgov berrygovt							 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)
	
	estimates store model3
	
outreg2 using 																 ///
	"/Users/Travis/Dropbox/Research/Judicial Selection and Criminal Punishment/Figures/v3 Figures/Table4.txt",   ///
	word nonotes title(Table 4: Antecedents of State Sentencing Rate)    	 ///
	lab bdec(2)																 ///
	onecol ctitle("Model 3")												 /// 
	alpha (.01, .05, .1) symbol(***, **, *) 								 ///
	addnote (All models estimated using Stata 15.0's xtpcse command. Data    ///
	from National Prisoner Statistics, 1978-2015, US Department of Justice,  ///
	ICPSR Study 36657. DV in all models is a state's incarceration rate		 ///
	per 100,000 population. Correlated panels corrected standard errors in 	 ///
	parentheses. Results of two–tailed tests of significance are as 	 	 ///
	follows: *p< .1 **p< .05 ***p< .01. Autocorrelation corrected using      ///
	panel-specific AR(1). Unit of analysis is the state-year.) replace
	
xtpcse sentencerate_change i.retentionelex##c.berrycitizen_l3a 		 		 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm sentencerate_change_l1	 ///
	electedpros termlength recall divgov berrygovt							 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)
	
	estimates store model4
	
outreg2 using 																 ///
	"/Users/Travis/Dropbox/Research/Judicial Selection and Criminal Punishment/Figures/v3 Figures/Table4.txt",   ///
	word nonotes title(Table 4: Antecedents of State Sentencing Rate)    	 ///
	lab bdec(2)																 ///
	onecol ctitle("Model 4")												 /// 
	alpha (.01, .05, .1) symbol(***, **, *) 								 ///
	addnote (All models estimated using Stata 15.0's xtpcse command. Data    ///
	from National Prisoner Statistics, 1978-2015, US Department of Justice,  ///
	ICPSR Study 36657. DV in all models is a state's incarceration rate		 ///
	per 100,000 population. Correlated panels corrected standard errors in 	 ///
	parentheses. Results of two–tailed tests of significance are as 	 	 ///
	follows: *p< .1 **p< .05 ***p< .01. Autocorrelation corrected using      ///
	panel-specific AR(1). Unit of analysis is the state-year.) append

*** Margins
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(1)1))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(2)2))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(0(3)3))
	margins, at(berrycitizen_l3a=(0 1) retentionelex=(2(1)3))

*** Contrasts

*contrasts (are the differences between selection type signif diff at .05 level?)
*comparing appt vs partisan
	margins r(0(1)1).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F6a)
	margins r(0(2)2).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F6b)
	margins r(0(3)3).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish 
		marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F6c)
		
	graph combine F6a.gph F6b.gph F6c.gph, saving(Figure6)
	 
******************************************************
*** Non-Partisan v. Partisan Elections: Sentencing ***
******************************************************
	
xtpcse sentencerate_change i.retentionelex##c.berrycitizen_l3a 		 ///
	arrestrate_l1 noiac dsl mand_min sentencecomm sentencerate_change_l1													 ///
	electedpros termlength recall divgov berrygovt										 ///
	south blackpop_log c.blackpop_log#c.blackpop_log bach_log stateincome_l1 ///
	if stateid!=11 & berrycitizen_l3a!=., p correlation(p)

margins r(2(1)3).retentionelex, at(berrycitizen_l3a=(0(.25)1) ) contrast(nowald effects) vsquish
	marginsplot, bydimension(retentionelex) yline(0) recast(line) recastci(rarea) l(95) ciopts(color(*.4)) saving(F7)
	
	
*** End of Analysis ***
	
log close

*** End of file
